package com.example.lib_base.base;

import android.os.Bundle;
import android.util.SparseArray;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.lib_base.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/*
* 一个项目无论提供哪个基础activity或fragment,主要看自己封装和自己业务需求
*
* baseActivity或baseFragment都是一样的
*
* 关于纯MVVM开发时 dataBinding出现报空的问题,主要还是横竖屏切换，如果限制只能横或竖屏的话用binding获取实例也可以
*
* 如果只在layout布局中进行绑定数据的话推荐这个，类似Compose声明式UI,不过还是感觉有些麻烦
*
* https://github.com/KunMinX/Strict-DataBinding  开源地址
* */
public abstract class DataBindingActivity extends AppCompatActivity {

    private ViewDataBinding mBinding;

    //如果使用dataBinding来找view实例就会显示该段提示信息
    private TextView mTvStrictModeTip;

    public DataBindingActivity() {
    }

    protected abstract void initViewModel();

    /**
     * 初始化
     */
    protected abstract void initViewOrObserve();

    /**
     * 主要设置资源id
     * @return 返回dataBindingConfig
     */
    protected abstract DataBindingConfig getDataBindingConfig();


    /**
     * TODO tip: 警惕使用。非必要情况下，尽可能不在子类中拿到 binding 实例乃至获取 view 实例。使用即埋下隐患。
     * 目前的方案是在 debug 模式下，对获取实例的情况给予提示。
     * <p>
     *
     * @return binding
     */
    protected ViewDataBinding getBinding() {
//        if (this.isDebug() && this.mBinding != null && this.mTvStrictModeTip == null) {
//            this.mTvStrictModeTip = new TextView(this.getApplicationContext());
//            this.mTvStrictModeTip.setAlpha(0.4F);
//            this.mTvStrictModeTip.setPadding(this.mTvStrictModeTip.getPaddingLeft() + 24, this.mTvStrictModeTip.getPaddingTop() + 64, this.mTvStrictModeTip.getPaddingRight() + 24, this.mTvStrictModeTip.getPaddingBottom() + 24);
//            this.mTvStrictModeTip.setGravity(1);
//            this.mTvStrictModeTip.setTextSize(10.0F);
//            this.mTvStrictModeTip.setBackgroundColor(-1);
//            String tip = this.getString(R.string.debug_databinding_warning, new Object[]{this.getClass().getSimpleName()});
//            this.mTvStrictModeTip.setText(tip);
//            ((ViewGroup)this.mBinding.getRoot()).addView(this.mTvStrictModeTip);
//        }
        return this.mBinding;
    }

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.initViewModel();
        DataBindingConfig dataBindingConfig = this.getDataBindingConfig();
        ViewDataBinding binding = DataBindingUtil.setContentView(this, dataBindingConfig.getLayout());

        binding.setLifecycleOwner(this);

        //不设置空viewModel
        if (dataBindingConfig.getStateViewModel() != null)
        binding.setVariable(dataBindingConfig.getVmVariableId(), dataBindingConfig.getStateViewModel());
        SparseArray<Object> bindingParams = dataBindingConfig.getBindingParams();
        int i = 0;

        //给binding设置Variable  BR.xxx为在layout中name
        for(int length = bindingParams.size(); i < length; ++i) {
            binding.setVariable(bindingParams.keyAt(i), bindingParams.valueAt(i));
        }
        this.mBinding = binding;
        initViewOrObserve();
    }

    public boolean isDebug() {
        return this.getApplicationContext().getApplicationInfo() != null && (this.getApplicationContext().getApplicationInfo().flags & 2) != 0;
    }

    protected void onDestroy() {
        super.onDestroy();
        this.mBinding.unbind();
        this.mBinding = null;
    }
}
